.<?php

/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2018-06-23
 * Time: 上午 10:20
 */
class Payment extends CI_Controller
{
    public $user;
    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');
        $this->user = $this->session->userdata('user_info');
        if(empty($this->user)){
            t_( "<script> location.href = '/login';</script>" );
            exit();
        }
    }

    function fee(){
        $this->load->view('weui/base');
        $this->load->view('weui/fee');
    }
    function charge(){
        $this->load->view('weui/base');
        $this->load->view('weui/charge');
    }
    function withdraw(){
        $this->load->view('weui/base');
        $this->load->view('weui/withdraw');
    }
    function fee_detail(){
        $this->load->view('weui/base');
        $this->load->view('weui/fee_detail');
    }

    private function load_weixinpub(){
        define("WEIXIN_APPID", HAO_WEIXIN_APPID);
        define("WEIXIN_APPSECRET", HAO_WEIXIN_APPSECRET);
        define("WEIXIN_MCHID", HAO_WEIXIN_MCHID);
        define("WEIXIN_KEY", HAO_WEIXIN_KEY);
        define("WEIXIN_SSLCERT_PATH", HAO_WEIXIN_KEY);
        define("WEIXIN_SSLKEY_PATH", HAO_WEIXIN_KEY);
        $this->load->library('WxPayPubHelper/SDKRuntimeException.php');
        $this->load->library('WxPayPubHelper/WxPaypubconfig.php');
        $this->load->library('WxPayPubHelper/WxPayPubHelper.php');
    }
    private function weixin_pay_res($openid=0,$codeno = 0,$fee=0){
        //使用统一支付接口
        $unifiedOrder = new \UnifiedOrder_pub();
        $unifiedOrder->setParameter("openid", "$openid");//商品描述
        $unifiedOrder->setParameter("body", "订单付款");//商品描述
        //自定义订单号，此处仅作举例
        $unifiedOrder->setParameter("out_trade_no", $codeno);//商户订单号
        $unifiedOrder->setParameter("total_fee", $fee);//总金额
        $unifiedOrder->setParameter("notify_url", "https://www.huipaibang.com/manage");//通知地址
        $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型

        $prepay_id = $unifiedOrder->getPrepayId();
        return $prepay_id;
    }

    /**
     * 支付
     * @return int
     */
    function pay(){
            $codeno = $this->input->get('code');
            $fee = $this->input->get('fee');
           $this->load_weixinpub();
            //使用jsapi接口
            $jsApi = new \JsApi_pub();
            $openid = $this->user->openid;
            $pay_cash = $fee;
            $fee = $fee * 100;
            $prepay_id = $this->weixin_pay_res($openid,$codeno,$fee);

            //=========步骤3：使用jsapi调起支付============
            $jsApi->setPrepayId($prepay_id);
            $jsApiParameters = $jsApi->getParameters();

            $data = [
                "catename"=>"微信支付",
                "pay_cash"=>$pay_cash,
                "jsApiParameters"=>$jsApiParameters,
            ];
            $this->load->library('parser');
            $html = $this->parser->parse("weixin_hao_pay",$data);
            return t_($html);
    }

    /**
     * 提现
     */
    function tixian_api(){
        $this->load_weixinpub();
        //使用统一支付接口
        $unifiedOrder = new \Refund_pub();
        $unifiedOrder->setParameter("out_trade_no", "out_trade_no");
        $unifiedOrder->setParameter("out_refund_no", "out_refund_no");
        $unifiedOrder->setParameter("total_fee", 10);
        $unifiedOrder->setParameter("refund_fee", 10);
        $unifiedOrder->setParameter("op_user_id", $this->user->openid);
        $result = $unifiedOrder->getResult();
        var_dump($result);die;
    }
}